Preliminaries
Hello, today we’re going to walk you through the process of installing CouchDB and ElasticSearch on your Ubuntu 14.04 Stack. CouchDB is one of the most widely used and versatile NoSQL database management systems. Its perfect when you need to store database entries as flexible objects rather than entries following a rigid schema. We’ll then show you how to interact with your CouchDB database using ElasticSearch. Elastic Search is a full-text search engine that has a REST web API. It works particularly well when you need to search schema-free JSON, which is exactly what we have with CouchDB. Without further ado, let’s get started.
We’ll assume that you’ve already followed our “Getting Started with Your Stack” tutorial and have set up a non-root user with sudo privileges.
Installing CouchDB
We’ll first need to install some dependencies found in the software-properties-common package and also add a third-party repository from the CouchDB developers.
sudo apt-get install software-properties-common sudo add-apt-repository ppa:couchdb/stable
We need to update our package list so the aptitude package manager discovers the new packages from the CouchDB developers.
sudo apt-get update
Finally, we can safely install the CouchDB package from the updated repositories with the confidence that we’re downloading the most recent stable version available to us.
sudo apt-get install couchdb
Install Elasticsearch
We need to install some dependencies for Elasticsearch before we can install the actual package. The package requires a working Java environment so we’ll go ahead and install that from the repositories.
sudo apt-get install openjdk-7-jre-headless
Now we can download and install Elasticsearch by directly downloading the .deb file from the distributor.
curl -O https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.5.2.deb dpkg -i elasticsearch-1.5.2.deb
Once this is done, we can configure the initialization settings so that Elasticsearch is enabled every time the machine is booted up. Run the following command:
sudo update-rc.d elasticsearch defaults 95 10
Once that’s done, we can attempt to start the Elasticsearch daemon and make sure everything is proceeding smoothly.
sudo service elasticsearch start
Now we need to configure Elasticsearch so that we can only use it locally. Otherwise anyone with our hostname could get data since Elasticsearch has no built in security. Open the ‘elasticsearch.yml’ file using nano to make some changes.
sudo vim /etc/elasticsearch/elasticsearch.yml
Look for the link that should be commented out and reads network.bind_host: localhost. You should uncomment this line and right below it add the following line.
script.disable_dynamic: true
We’re done configuring this file so hit CTRL-O, Return/Enter, followed by CTRL-X to write changes to file and exit. We’ll need to restart the Elasticsearch daemon in order to allow the changes to take place.
sudo service elasticsearch restart
Finally, we can test that Elasticsearch is working by using cURL to send a GET request to the REST API. The output of this command should have some very basic information about your Elasticsearch installation formatted as a JSON object.
curl http://127.0.0.1:9200
Connecting ElasticSearch and CouchDB
Now we need to configure elasticsearch to work with CouchDB. We’ll be installing the CouchDB River plugin. We’ll stop the Elasticsearch daemon for now in order to make sure all of our changes are implemented correctly before starting the service up again.
sudo service elasticsearch stop
Now navigate to the folder where the plugin installer executable is located:
cd /usr/share/elasticsearch
We’ll be installing the elasticsearch-river-couchdb plugin which allows for communication between CouchDB and Elasticsearch.
sudo ./bin/plugin -install elasticsearch/elasticsearch-river-couchdb/2.5.0
When that’s complete we’ll be able to start Elasticsearch again:
sudo service elasticsearch start
Now we can send a PUT request to the CouchDB server to create a DB
curl -XPUT http://127.0.0.1:5984/[dbname]
Filling in [dbname] appropriately. Let’s write some data to the database so we can test out the functionality of Elasticsearch and verify that we have a valid installation of CouchDB. Once again, we’ll interact with CouchDB’s REST API and use PUT requests to insert JSON documents into the database we just created.
curl -XPUT 'http://127.0.0.1:5984/[dbname]/1' -d '{"name": "One Value"}'
curl -XPUT 'http://127.0.0.1:5984/[dbname]/2' -d '{"name": "Two Value"}'
curl -XPUT 'http://127.0.0.1:5984/[dbname]/3' -d '{"name": "Three Value"}'
curl -XPUT 'http://127.0.0.1:5984/[dbname]/4' -d '{"name": "Four Value"}'
curl -XPUT 'http://127.0.0.1:5984/[dbname]/5' -d '{"name": "Five Value"}'
curl -XPUT 'http://127.0.0.1:5984/[dbname]/6' -d '{"name": "Six Value"}'
We have just one more configuration step to get Elasticsearch to work seamlessly with CouchDB. We need to create an index for Elasticsearch. Elasticsearch stores data according to an index and a type. we’ll name our index and type the same as the database for simplicity
curl -XPUT '127.0.0.1:9200/_river/[dbname]/_meta' -d '{ "type" : "couchdb", "couchdb" : { "host" : "localhost", "port" : 5984, "db" : "[dbname]", "filter" : null }, "index" : { "index" : "[dbname]", "type" : "[dbname]", "bulk_size" : "100", "bulk_timeout" : "10ms" } }'
If everything proceeded correctly, we should be set to run queries on our data again using cURL to interact with the REST API.
curl http://127.0.0.1:9200/testdb/testdb/_search?pretty=true
This will pretty print all the data you entered into your CouchDB database earlier. You can now put in whatever data you’d like and see if Elasticsearch really is blazing fast.
Final Words
Congratulations! You have Elasticsearch and CouchDB configured such that you can put just about any sort of structured or unstructured data into a CouchDB database using JSON and Elasticsearch will be able to rip through the database and extract just the information you need. For more information about CouchDB and Elasticsearch, check our the documentation here and here. From all of us at Stack Harbor, ahoy!